﻿using NUnit.Framework;

namespace fluentvaly.Tests.ValidationStatementTests
{
    [TestFixture]
    public class IfExpressinoFixture
    {
        private const string ERROR_MESSAGE = "Error message";
        private const string PASSWORD = "password";

        [Test]
        public void If_ExpressionGiven_ValidatorCanBeExecuted()
        {
            var validator = new MemberValidator<UserTestObject, string>(u => u.Password, ERROR_MESSAGE, InvalidationReasons.Missing);
            var user = new UserTestObject {Password = PASSWORD, AcceptyAGBs = true};

            validator.If((u, o) => u.AcceptyAGBs);

            Assert.That(((IValidationStatement)validator).CantRun(user), Is.True);
        }

        [Test]
        public void If_ExpressionGiven_ValidationCanNotBeExecuted()
        {
            var validator = new MemberValidator<UserTestObject, string>(u => u.Password, ERROR_MESSAGE, InvalidationReasons.Missing);
            var user = new UserTestObject { Password = PASSWORD, AcceptyAGBs = false };

            validator.If((u, o) => u.AcceptyAGBs);

            Assert.That(((IValidationStatement)validator).CantRun(user), Is.False);
        }

        [Test]
        public void If_ExpressionIsNotGiven_ValidationCanBeExecuted()
        {
            var validator = new MemberValidator<UserTestObject, string>(u => u.Password, ERROR_MESSAGE, InvalidationReasons.Missing);
            var user = new UserTestObject { Password = PASSWORD, AcceptyAGBs = false };

            Assert.That(((IValidationStatement)validator).CantRun(user), Is.True);
        }

        [Test]
        public void If_DependendExpressionGiven_ValidationCanBeExecuted()
        {
            var validator = new MemberValidator<UserTestObject, string>(u => u.Password, ERROR_MESSAGE, InvalidationReasons.Missing);
            var user = new UserTestObject { Password = PASSWORD, PasswordConfirmation = PASSWORD };

            validator.If((u, pass) => u.PasswordConfirmation == pass);

            Assert.That(((IValidationStatement)validator).CantRun(user), Is.True);
        }

        [Test]
        public void If_DependendExpressionGiven_ValidationCanNotBeExecuted()
        {
            var validator = new MemberValidator<UserTestObject, string>(u => u.Password, ERROR_MESSAGE, InvalidationReasons.Missing);
            var user = new UserTestObject { Password = PASSWORD, PasswordConfirmation = string.Empty };

            validator.If((u, pass) => u.PasswordConfirmation == pass);

            Assert.That(((IValidationStatement)validator).CantRun(user), Is.False);
        }
    }
}
